package jp.sourceforge.talisman.mds.distance;

/*
 * $Id: AbstractItemDistanceCalculator.java,v 1.1 2009/09/12 05:06:07 weiwei Exp $
 */

import jp.sourceforge.talisman.mds.Item;

/**
 * 
 * @author Haruaki Tamada
 * @version $Revision: 1.1 $ 
 */
abstract class AbstractItemDistanceCalculator implements ItemDistanceCalculator{

    public double calculate(Item x, Item y){
        int lenX = x.getDimension();
        int lenY = y.getDimension();
        int length = lenX;
        if(lenX > lenY){
            length = lenY;
        }
        return calculate(x, y, length);
    }

    public double calculate(Item x, Item y, int length){
        double[] xarray = new double[length];
        double[] yarray = new double[length];
        for(int i = 0; i < length; i++){
            xarray[i] = x.get(i).doubleValue();
            yarray[i] = y.get(i).doubleValue();
        }
        return calculate(xarray, yarray);
    }

    public abstract double calculate(double[] x, double[] y);

    public abstract ItemDistanceAlgorithm getAlgorithm();

    protected boolean validateArguments(double[] x, double[] y){
        if(x.length != y.length){
            throw new IllegalArgumentException(
                "difference array size: " + x.length + " != " + y.length
            );
        }
        return true;
    }
}
